<template>
  <div class="brand_page">
    <form enctype="multipart/form-data" id="uploadForm">
      <div class="row">
        <h3>顶部标题编辑</h3>
        <textarea id="editor1" class="ckeditor" rows="10" cols="80">
          ......
        </textarea>
      </div>
      <div class="row">
        <h3>大图上传</h3>
        <input type="file" name="big_img" id="big_img">
        <div class="row_height">
          <div class="col-md-4">
            <div class="img_box">
              <img :src="big_img" alt="" class="full-width">
            </div>
          </div>
        </div>
      </div>
      <div class="row">
        <h3>大图上文字编辑</h3>
        <textarea id="editor2" class="ckeditor" rows="10" cols="80">
          ......
        </textarea>
        <el-button type="primary" style="margin:20px 0 0 0" @click="uploadData()">提交</el-button>
      </div>
    </form>
  </div>
</template>
<script>
import g from '../../components/global.js'
export default {
  data() {
    return {
      big_img: ''
    }
  },
  mounted() {
    CKEDITOR.replace('editor1')
    CKEDITOR.replace('editor2')
    this.getData()
  },
  methods: {
    uploadData() {
      var that = this
      var formtxt1 = CKEDITOR.instances.editor1.getData()
      var formtxt2 = CKEDITOR.instances.editor2.getData()
      var formData = new FormData()
      if ($('#big_img').val() == '') {
      } else {
        formData.append('big_img', $('#big_img')[0].files[0])
      }
      formData.append('head_tle', formtxt1)
      formData.append('img_txt', formtxt2)
      $.ajax({
        url: g.res_url + '/api/v1/brand_save',
        type: 'POST',
        data: formData,
        async: false,
        cache: false,
        contentType: false,
        processData: false,
        success: function(data) {
          if (data == 'success') {
            that.$message({
              message: '操作成功！',
              type: 'success'
            })
            that.getData()
          } else {
            that.$message.error('操作失败！')
          }
        },
        error: function(returndata) {
          that.$message.error('网络繁忙！')
        }
      })
    },
    getData() {
      var _this = this
      $.ajax({
        type: 'post',
        url: g.res_url + '/api/v1/brand_getData',
        success: function(data) {
          var dataObjs = JSON.parse(data)
          var dataObj = dataObjs.data[0]
          setTimeout(function() {
            CKEDITOR.instances.editor1.setData(dataObj.head_tle)
            CKEDITOR.instances.editor2.setData(dataObj.img_txt)
          }, 1000)
          _this.big_img = dataObj.big_img
        }
      })
    }
  }
}
</script>
<style scoped>
.row_height {
  height: 150px !important;
  margin: 28px 0;
}
.btn_box {
  margin-left: 45px;
}
.img_box {
  width: 200px;
  height: 200px;
  overflow: hidden;
  margin-bottom: 20px;
}
.full-width {
  width: 100%;
}
.col-md-4 {
  margin-top: 30px;
}
.brand_page {
  padding: 0 0 50px 20px;
}
</style>

